/**
 * Copyright DingLian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.topchain.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.time.LocalDateTime;



/**
 * class description
 *
 * @author wanliangbing
 * @date 18-11-13
 * @company DingLian
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultMessage<T> implements Serializable {

    @ApiModelProperty(value = "是否成功的标志")
    private boolean success = true;
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "描述信息")
    private String message;
    @ApiModelProperty(value = "返回的数据")
    private T data;
    @ApiModelProperty(value = "当前时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private LocalDateTime timestamp = LocalDateTime.now();

    public ResultMessage() {
        this(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMessage(), null);
    }

    public ResultMessage(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // -------- 成功时,可以使用如下三个方法返回成功消息 -------- //
    // 成功,返回体中没有数据. 成功的消息使用系统预先定义好的
    public static <T> ResultMessage<T> success() {
        return new ResultMessage<>();
    }

    // 成功,返回体中有数据. 成功的消息使用系统预先定义好的
    public static <T> ResultMessage<T> success(T data) {
        ResultMessage<T> resultMessage = new ResultMessage<>();
        resultMessage.setMessage(ResponseCodeEnum.SUCCESS.getMessage());
        resultMessage.setData(data);
        return resultMessage;
    }

    // 成功,返回体中有数据. 但是成功的消息我想自定义
    public static <T> ResultMessage<T> success(String message, T data) {
        ResultMessage<T> resultMessage = new ResultMessage<>();
        resultMessage.setMessage(message);
        resultMessage.setData(data);
        return resultMessage;
    }

    public static <T> ResultMessage<T> success(String message, boolean success) {
        ResultMessage<T> resultMessage = new ResultMessage<>();
        resultMessage.setMessage(message);
        resultMessage.setSuccess(success);
        return resultMessage;
    }

    // -------- 失败时,可以使用如下两个方法返回失败消息 -------- //
    // 失败时返回的信息,其data属性肯定没有值(使用系统预先定义好的枚举值)
    public static <T> ResultMessage<T> errorResult(ResponseCodeEnum responseCodeEnum) {
        ResultMessage<T> resultMessage = new ResultMessage<>(responseCodeEnum.getCode(), responseCodeEnum.getMessage(), null);
        resultMessage.setSuccess(false);
        return resultMessage;
    }

    // 失败时返回的信息,其data属性肯定没有值 (临时自定义异常错误码和错误信息)
    public static <T> ResultMessage<T> errorResult(Integer code, String message) {
        ResultMessage<T> resultMessage = new ResultMessage<>(code, message, null);
        resultMessage.setSuccess(false);
        return resultMessage;
    }
    
    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }
}
